package com.sy.simpleegg.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.sy.simpleegg.bean.GoodBean
import com.sy.simpleegg.bean.RegDataBean
import com.sy.simpleegg.databinding.ItemRegcodeBinding
import com.sy.simpleegg.util.QRCodeUtil

class RegCodeAdapter(
    private var mContext: Context,
    private var regCodeList: MutableList<RegDataBean>
) : RecyclerView.Adapter<RegCodeAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RegCodeAdapter.ViewHolder {
        val binding = ItemRegcodeBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    @SuppressLint("NewApi", "SetTextI18n", "UseCompatLoadingForDrawables")
    override fun onBindViewHolder(holder: RegCodeAdapter.ViewHolder, position: Int) {
        val goodBean: RegDataBean = regCodeList[position]

        if (goodBean.code.isNotEmpty() && goodBean.status == 1) {
            holder.pbRegCode.visibility = View.GONE
            holder.ivRegCode.visibility = View.VISIBLE
            Glide.with(mContext)
                .load(QRCodeUtil.createQRCode(goodBean.code, 500, 500))
                .fitCenter()
                .into(holder.ivRegCode)

        } else {
            holder.pbRegCode.visibility = View.VISIBLE
            holder.ivRegCode.visibility = View.GONE
        }

        holder.tvTips.text = goodBean.platform
    }

    override fun getItemCount(): Int {
        return regCodeList.size
    }

    inner class ViewHolder(binding: ItemRegcodeBinding) : RecyclerView.ViewHolder(binding.root) {
        val pbRegCode: ProgressBar = binding.pbRegcode
        val ivRegCode: ImageView = binding.ivRegistCode
        val tvTips: TextView = binding.tvRegTips
    }


    fun setData(newList: MutableList<RegDataBean>) {
        regCodeList = newList
        notifyDataSetChanged()
    }


    interface OnItemClickListener {
        fun onClick(goodBean: GoodBean?)
    }

    private var mOnClickListener: OnItemClickListener? = null
    fun setOnItemClickListener(onItemClickListener: OnItemClickListener?) {
        mOnClickListener = onItemClickListener
    }
}